//
//  CYDiscoverViewController.m
//  cyjf
//
//  Created by tauCross on 2017/11/27.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYDiscoverViewController.h"

#import "CYDiscoverCullCell.h"
#import "CYEmptyCell.h"
#import "CYDiscoverTitleCell.h"
#import "CYDiscoverEndCell.h"
#import "CYDiscoverCell.h"

#import "CYActionManager.h"

#import <UIScrollView+TCRefresh.h>



@interface CYDiscoverViewController ()

@property(nonatomic, strong)CYDiscoverCullCellObject *cullCell;
@property(nonatomic, strong)CYDiscoverTitleCellObject *titleCell;
@property(nonatomic, strong)NSMutableArray *cells;
@property(nonatomic, strong)CYDiscoverEndCellObject *endCell;

@property(nonatomic, assign)NSInteger pageNo;
@property(nonatomic, assign)BOOL loading;
@property(nonatomic, assign)BOOL hasMore;
@property(nonatomic, strong)NSString *requestID;

@end


@implementation CYDiscoverViewController

- (void)loadUI
{
    self.navigationItem.title = @"发现";
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 13)];
    
    @weakify(self);
    [self.tableView setupRefreshWithBottomAt:0 refreshBlock:^{
        @strongify(self);
        self.requestID = [NSUUID UUID].UUIDString;
        self.loading = NO;
        self.pageNo = 1;
        self.hasMore = YES;
        [self loadDataFromServer];
    }];
}

- (void)loadData
{
    self.pageNo = 1;
    self.loading = NO;
    self.hasMore = YES;
    self.requestID = [NSUUID UUID].UUIDString;
    self.cells = [NSMutableArray array];
    @weakify(self);
    self.cullCell = [CYDiscoverCullCellObject objectWithCellClass:[CYDiscoverCullCell class]];
    self.cullCell.onClickModel = ^(CYDataAPIAdvertisementModel *model) {
        @strongify(self);
        [self actionWithModel:model];
    };
    
    self.titleCell = [CYDiscoverTitleCellObject objectWithCellClass:[CYDiscoverTitleCell class]];
    
    self.endCell = [CYDiscoverEndCellObject objectWithCellClass:[CYDiscoverEndCell class]];
    
    [self loadDataFromServer];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadCullFromServer];
}

- (void)loadCullFromServer
{
    @weakify(self);
    [CYStandardDataAPI advertisement_getListWithPosition:CYDataAPIAdvertisementPositionDiscoverCull page_no:nil complete:^(CYResponse *response) {
        if(response.code == 0)
        {
            if([response.content isKindOfClass:[NSArray class]] && [response.content count] > 1)
            {
                NSArray *models = [CYDataAPIAdvertisementModel mj_objectArrayWithKeyValuesArray:response.content];
                @strongify(self);
                self.cullCell.models = models;
                [self reloadTableView];
            }
        }
    }];
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    if(self.hasMore == NO)
    {
        return;
    }
    self.loading = YES;
    NSString *requestID = self.requestID;
    @weakify(self);
    [CYStandardDataAPI advertisement_recentActivitiesWithPage_no:@(self.pageNo) complete:^(CYResponse *response) {
        @strongify(self);
        if([self.requestID isEqualToString:requestID])
        {
            if(response.code == 0)
            {
                if([response.content isKindOfClass:[NSArray class]])
                {
                    NSArray *models = [CYDataAPIAdvertisementModel mj_objectArrayWithKeyValuesArray:response.content];
                    self.hasMore = models.count >= CY_API_PAGE_SIZE;
                    if(self.pageNo == 1)
                    {
                        [self.cells removeAllObjects];
                    }
                    for(CYDataAPIAdvertisementModel *model in models)
                    {
                        CYDiscoverCellObject *cell = [CYDiscoverCellObject objectWithCellClass:[CYDiscoverCell class]];
                        cell.model = model;
                        [self.cells addObject:cell];
                    }
                    self.pageNo ++;
                    [self reloadTableView];
                }
            }
            self.loading = NO;
            [self.tableView endRefresh];
        }
    }];
}

- (void)reloadTableView
{
    NSMutableArray *cells = [NSMutableArray array];
    {
        [cells addObject:self.cullCell];
    }
    {
        CYEmptyCellObject *cell = [CYEmptyCellObject objectWithCellClass:[CYEmptyCell class]];
        cell.height = 10;
        [cells addObject:cell];
    }
    if(self.cells.count)
    {
        {
            [cells addObject:self.titleCell];
        }
        {
            [cells addObjectsFromArray:self.cells];
        }
        if(self.hasMore == NO)
        {
            [cells addObject:self.endCell];
        }
    }
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:cells delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.tableView)
    {
        if(scrollView.contentSize.height - scrollView.contentOffset.y < scrollView.height * 2 && self.pageNo != 1)
        {
            [self loadDataFromServer];
        }
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CYDiscoverCellObject *cell = [self.tableModel objectAtIndexPath:indexPath];
    if([cell isKindOfClass:[CYDiscoverCellObject class]])
    {
        [self actionWithModel:cell.model];
        return;
    }
}

#pragma mark - Actions
- (void)actionWithModel:(CYDataAPIAdvertisementModel *)model
{
    [CYStandardActionManager executeActionWithModel:model.action fromViewController:self];
}

@end
